<?php

/**
 * Glossary
 * @author SilverBiology
 * @link http://www.silverbiology.com
 */

include_once('class.mysqli_database.php');

Class SavedSearch
{
	public function __construct() {

		global $mysql_host, $mysql_db, $mysql_user, $mysql_pass;
		$this->connection_string = sprintf("server=%s; database=%s; username=%s; password=%s; charset=UTF8;"
			,	$mysql_host
			, $mysql_db
			,	$mysql_user
			,	$mysql_pass
		);

		$this->db = new MysqliDatabase( $this->connection_string);
		$this->db->query_all('SET CHARACTER SET UTF8');
	}
	public function get($property) {
		return (isset($this->{$property})) ? $this->{$property} : false;
	}
	public function set($property,$value) {
		$this->{$property} = $value;
		return true;
	}
	public function save() {
		$query = sprintf("INSERT INTO `savedSearch` SET `query` = '%s', `searchType` = '%s',`dateAdded` = NOW();"
		, mysql_escape_string($this->get('query'))
		, mysql_escape_string($this->get('searchType')));
		return ($this->db->query($query));
	}
	public function listSavedSearch($filter) {
		$groupBy = '';
		$query = 'SELECT SQL_CALC_FOUND_ROWS s.*, count(a.`association`) associationCount FROM `savedSearch` s LEFT OUTER JOIN `associations` a ON s.`query` = a.`term` ';
		switch($filter['assocFlag']) {
			case 'true':
				$groupBy = ' GROUP BY s.`autoId` HAVING associationCount > 0 ';
				break;
			case 'false':
				$groupBy = ' GROUP BY s.`autoId` HAVING associationCount = 0 ';
				break;
			default:
				$groupBy = ' GROUP BY s.`autoId` ';
				break;
		}
		$where = ' WHERE 0=0 ';
		if($filter['query'] != '') {
			if($filter['type'] == 'startsWith') {
				$where .= sprintf(" AND s.`query` LIKE '%s%%' ", mysql_escape_string($filter['query']));
			} else {
				$where .= sprintf(" AND s.`query` = '%s' ", mysql_escape_string($filter['query']));
			}
		}
		if($filter['searchType'] != '') {
			$where .= sprintf(" AND s.`searchType` = '%s' ", mysql_escape_string($filter['searchType']));
		}
		$query = $query . $where . $groupBy;

		$query .= build_order( $filter['order'] );
		$query .= build_limit($filter['start'], $filter['limit']);

		return($this->db->query_all($query));
	}

# ---------

	public function listAssociation($filter) {
		$where = ' WHERE 0=0 ';
		if($filter['term'] != '') {
			$where .= sprintf(" AND `term` = '%s' ", mysql_escape_string($filter['term']));
		}
		if($filter['association'] != '') {
			$where .= sprintf(" AND `association` = '%s' ", mysql_escape_string($filter['association']));
		}
		$where .= build_order( $filter['order'] );
		$where .= build_limit($filter['start'], $filter['limit']);
		$query = 'SELECT SQL_CALC_FOUND_ROWS * FROM `associations` ' . $where;
		return($this->db->query_all($query));
	}

	public function addAssociation() {
		$query = sprintf("INSERT IGNORE INTO `associations` SET `term` = '%s', `association` = '%s';"
		, mysql_escape_string($this->get('term'))
		, mysql_escape_string($this->get('association')));
		return ($this->db->query($query));
	}

	public function deleteAssociation() {
		$query = sprintf("DELETE FROM `associations` WHERE `term` = '%s' AND `association` = '%s';"
		, mysql_escape_string($this->get('term'))
		, mysql_escape_string($this->get('association')));
		return ($this->db->query($query));
	}
}
?>